import pprint

from src.HandleKylinKYSA.Class_KylinKYSA import KylinKYSA
from globally import product_id


class KylinKYSAProcessor:
    ProductId = product_id

    def __init__(self, kysalist, productid, framework):
        self._kysalist = kysalist
        self._produtid = productid
        self._framework = framework

    def kysa_reports_filter(self) -> list:
        all_kysa_report_after_filter = []
        count = 1
        for kysa_number in self._kysalist:
            print(f"{count}/{len(self._kysalist)}: 获取{kysa_number}的公告信息")
            kysa_report = KylinKYSA(kysa_number).kysa_report
            count += 1
            # print(kysa_report)
            for kysa_data in kysa_report:
                if kysa_data["productid"] == self._produtid and kysa_data["framework"] == self._framework:
                    all_kysa_report_after_filter.append(kysa_data)
        return all_kysa_report_after_filter

    # @classmethod
    # def _record_kysareports2file(cls, all_kysa_reports):
    #     for kysa_data in all_kysa_reports:
    #         info = ("kysa_number: {}".format(kysa_data["kysa_number"]),
    #                 "productid: {}".format(kysa_data["productid"]),
    #                 "framework: {}".format(kysa_data["framework"]),
    #                 "packages:"
    #                 "{}".format(list(kysa_data["packages"].keys())))
    #         pprint.pprint(info)


    @classmethod
    def _kysareport_packages(cls, all_kysa_reports) -> list:
        datas = []
        for kysa_data in all_kysa_reports:
            packages = []
            for mumber in list(kysa_data["packages"].keys()):
                if mumber not in packages:   # mumber是包名+版本
                    packages.append(mumber)
            data = {
                "kysa_number": kysa_data["kysa_number"],
                "package_names": packages
            }
            datas.append(data)
        return datas

    @classmethod
    def _kysareport_packagesurl(cls, all_kysa_reports) -> list:
        urls = []
        for kysa_data in all_kysa_reports:
            for mumber in list(kysa_data["packages"].keys()):
                if kysa_data["packages"][mumber] not in urls:
                    urls.append(kysa_data["packages"][mumber])
        return urls

    @classmethod
    def _print_kysareports(cls, productid, framework, all_kysa_reports):
        print(f"========> 系统版本：{productid}  系统架构：{framework} <========")
        package_datas = cls._kysareport_packages(all_kysa_reports)
        for package_data in package_datas:
            print("编号：{}".format(package_data["kysa_number"]))
            print("涉及软件包：{}个".format(len(package_data["package_names"])))
            for package_name in package_data["package_names"]:
                print("    {}".format(package_name))
        #

        # 下载链接处理
        urls = cls._kysareport_packagesurl(all_kysa_reports)
        print("上述涉及的下载链接：{}个".format(len(urls)))
        for url in urls:
            print(f"    {url}")


    def processor(self):
        self._print_kysareports(KylinKYSAProcessor.ProductId[self._produtid], self._framework, self.kysa_reports_filter())



